package demo5;

public class Test {
    public static void main(String[] args) {
        String str = "hello";
        System.out.println(str.isEmpty());
        System.out.println(str.length());
    }
    public static void main15(String[] args) {
        long start = System.currentTimeMillis();
        StringBuilder stringBuilder = new StringBuilder("hello");
        for (int i = 0; i < 100000; i++) {
            stringBuilder.append("nihao");
        }
        long end = System.currentTimeMillis();
        System.out.println(end - start);
        String str = "fasdf";

    }
    public static void main14(String[] args) {
        String str = "hello";
        StringBuilder stringBuilder = new StringBuilder(str);
        stringBuilder.append("nihao");
        System.out.println(stringBuilder.toString());
        for (int i = 0; i < 100; i++) {
            str = str + "nihao";
        }
        System.out.println(str);
    }
    public static void main13(String[] args) {
        String str = "    hello  world   ";
        String s1 = str.trim();
        System.out.println(s1);//只会删除两边的空格
    }
    public static void main12(String[] args) {
        String str = "hellonn";
        //截取字符串
        String s = str.substring(2);
        System.out.println(s);

        String s1 = str.substring(2,5);
        System.out.println(s1);// 截取的时候是前闭后开   [ )
    }
    public static void main11(String[] args) {
        String str2 = "192|168*20.9";
        String[] split = str2.split("\\||\\*|\\.");
        for (String str:split) {
            System.out.println(str);
        }
    }

    public static void main10(String[] args) {
        String str1 = "192.168.20.55";
        String[] strs = str1.split("\\.");
        for (int i = 0; i < strs.length; i++) {
            System.out.print(strs[i] + " ");        //192 168 20 55
        }
        System.out.println();
        String str2 = "192\\168\\20\\55";
        String[] strs1 = str2.split("\\\\");
        for (int i = 0; i < strs1.length; i++) {
            System.out.print(strs1[i] + " ");       //192 168 20 55
        }
    }
    public static void main9(String[] args) {
        String str1 = "hello world Java";
        String[] strs = str1.split(" ",2);
        for (int i = 0; i < strs.length; i++) {
            System.out.println(strs[i]);
        }
    }
    public static void main8(String[] args) {
        String str1 = "asdfgfgfhs";
        String ret1 = str1.replace('g','w');  //asdfwfwfhs
        System.out.println(ret1);

        String ret2 = str1.replace("fg","pq");  //asdpqpqfhs
        System.out.println(ret2);

        String ret3 = str1.replaceAll("fg","oo");
        System.out.println(ret3);//asdoooofhs

        String ret4 = str1.replaceFirst("fg","oo"); //传入字符串
        System.out.println(ret4);//asdoofgfhs
    }
    public static void main7(String[] args) {
        String str1 = "abcdf";
        String str2 = "ABFDSF";
        System.out.println(str1.toUpperCase());//ABCDF
        System.out.println(str2.toLowerCase());//abfdsf
    }
    public static void main6(String[] args) {
        //数字转字符串
        int num = 12345;
        String str2 = String.valueOf(num);
        System.out.println(str2);

        //字符串转数字
        String str1 = "1234";
        Integer a = Integer.valueOf("12354");// 字符串转变为 Integer类型的
        System.out.println("=====" + a);
        int data = Integer.parseInt(str1);  // 字符串转变为  int类型
        System.out.println(data);//1234
        double data2 = Double.parseDouble(str1);  // 字符串转变为  double类型
        System.out.println(data2);//1234.0
    }
    public static void main5(String[] args) {
        String str1 = "helloeee";
        char ch = str1.charAt(1);
        System.out.println(ch);  //e

        System.out.println(str1.indexOf('e',4));  //找e  从指定位置(4)开始
        System.out.println(str1.indexOf("lloe")); //2
        System.out.println(str1.lastIndexOf('e'));//从后往前找
        System.out.println(str1.lastIndexOf('e',5));//从5下标(包括5下标)后往前找

        for (int i = 0; i < str1.length(); i++) {
            System.out.print(str1.charAt(i) + " ");
        }
    }
    public static void main4(String[] args) {
        String str = "abcd";
        String str1 = "ABCD";
        //忽略大小写判断字符串的大小
        System.out.println(str.compareToIgnoreCase(str1));
        //忽略大小写判断字符串是否相等
        System.out.println(str.equalsIgnoreCase(str1));
    }
    public static void main3(String[] args) {
        String str = new String("hello");
        String str1 = new String("hello");
        System.out.println(str.equals(str1));
    }
    public static void main2(String[] args) {
        //这种是str引用所指向的对象为空
        String str = "";

        //这个str1引用 = null  意思是它不指向任何对象
        String str1 = null;
    }
    public static void main1(String[] args) {
        //直接赋值
        String str = "hello";

        //通过String的构造方法赋值
        String str1 = new String("hello");

        //通过String的构造方法把字符数组的值赋值给String
        char[] chars = new char[]{'a','b','c','d','e'};
        String str2 = new String(chars);

    }
}
